/**加密地址:https://www.sojson.com/jsobfuscator.html**/
layui.use('layer', function () {
    var layer = layui.layer;  //layer初始化
    //代码风格语法高亮
    hljs.initHighlightingOnLoad();
    //详情查询
    queryBlogDetail(getReqUrl('id'));
    //右边导航栏
    initiRight(getReqUrl('id'));
    //最新品论
    queryBlogComment(getReqUrl('id'));

    $('#blog-signature').attr('href','https://www.sunnyblog.top/detail.html?id=' + getReqUrl('id'));
    $('#blog-signature').text('https://www.sunnyblog.top/detail.html?id=' + getReqUrl('id'));

});
/**
 * 查询博客明细
 * @param id
 */
function queryBlogDetail(id) {
    $.ajax({
        type: 'POST',
        url: '/blog/queryBlogDetail',
        dataType: 'json',
        data: JSON.stringify({'id': id}),
        contentType: 'application/json; charset=UTF-8',// 解决415错误
        success: function (res) {
            if('200' == res.code) {
                var detailData = res.data;
                var breadHtml = getBreadHtml(JSON.parse(decrypt(detailData,'17E0380EC0138697')));
                $('#blog-bread').html(breadHtml);
                var html = getDetailHtml(JSON.parse(decrypt(detailData,'17E0380EC0138697')));
                $('#blog-content').html(html);
                //分享
                $('#blog-share').share({sites: ['qzone', 'qq', 'weibo','wechat']});
            }
        }
    });
}

/**
 * 加载右侧侧边栏信息
 */
function initiRight(id) {
    //调用后台
    $.ajax({
        beforeSend: function() {
            layer.load(2);
        },
        type: 'POST',
        url: '/blog/queryDetailRightInfo',
        dataType: 'json',
        data: JSON.stringify({'id': id}),
        contentType: 'application/json; charset=UTF-8',// 解决415错误
        success: function (res) {
            if('200' == res.code) {
                layer.closeAll("loading");
                //分类信息
                initBlogType(res.data.blogTypeList);
                //相似文章
                initSimilar(res.data.similarList);
                //热文信息
                initRank(res.data.rankBeanList);

            }else {
                layer.closeAll("loading");
                layer.msg(res.msg, {icon: 5, time: 2000});
            }
        },
        error: function() {
            layer.closeAll("loading");
            layer.msg('系统繁忙请稍后重试!', {icon: 5, time: 2000});
        }
    });

}

//初始化热文信息
function initSimilar(similarData) {
    var html = '';
    html += '<div class="blog-module shadow">';
    html += '<div class="blog-module-title">相似文章</div>';
    html += '<ul class="fa-ul blog-module-ul">';
    $.each(similarData,function (k,v) {
        html += '<li><i class="fa-li fa fa-hand-o-right"></i><a href="detail.html?id='+v.id+'">'+v.title+'</a></li>';
    });
    html += '</ul>';
    html += '</div>';
    //加载热文信息
    $('.blog-main-right').append(html);
}

//初始化分类信息
function initBlogType(typeData) {
    var html = '';
    html += '<div class="article-category shadow">';
    html += '<div class="article-category-title">分类导航</div>';
    $.each(typeData,function (k,v) {
        html += '<a href="index.html?typeId='+v.typeId+'">'+v.typeName+'('+v.typeOrder+')</a>';
    });
    html += '<div class="clear"></div>';
    html += '</div>';
    //加载分类信息
    $('.blog-main-right').append(html);
}

//初始化热文信息
function initRank(rankData) {
    var html = '';
    html += '<div class="blog-module shadow">';
    html += '<div class="blog-module-title">最热阅读</div>';
    html += '<ul class="fa-ul blog-module-ul">';
    $.each(rankData,function (k,v) {
        html += '<li><i class="fa-li fa fa-hand-o-right"></i><a href="detail.html?id='+v.id+'">'+v.title+'</a></li>';
    });
    html += '</ul>';
    html += '</div>';
    //加载热文信息
    $('.blog-main-right').append(html);
}

/**
 * 导航指引
 * @param detailData
 */
function getBreadHtml(detailData) {
    var breadHtml = '';
    $.each(detailData,function (key,val) {
        breadHtml += '<a title="首页" href="index.html"> <i class="fa fa-home fa-2x"></i></a>';
        breadHtml += '<small>&gt;</small>';
        breadHtml += '<a href="index.html?typeId='+val.typeId+'" style="font-size: 17px">'+val.typeName+'</a>';
        breadHtml += '<small>&gt;</small>';
        breadHtml += '<span class="muted" style="font-size: 17px">'+val.title+'</span>';
    });
    return breadHtml;
}
/**
 * 获取详情信息
 * @param detailData
 * @returns {string}
 */
function getDetailHtml(detailData) {
    var html = '';
    $.each(detailData,function (key,val) {
        $('title').html(val.title + '_SunnyBlog博客系统-Powered by sunnyblog.top');
        $('meta[name="keywords"]').attr('content',val.title + '_SunnyBlog博客系统-Powered by sunnyblog.top');
        $('meta[name="description"]').attr('content',val.summary + '_SunnyBlog博客系统-Powered by sunnyblog.top');
        html += '<div class="article-detail shadow">';
        html += '<div class="article-detail-title">'+val.title+'</div>';
        //文章简要信息
        html += '<div class="article-detail-info">';
        html += '<span>发布时间：『'+timeStamp2String(val.releaseDate)+'』</span>';
        html += '<span>博客类别：'+val.typeName+'</span>';
        html += '<span>作者：sunny</span>';
        html += '<span>阅读量：('+val.browseNum+')</span>';
        html += '</div>';
        html += '<div id="blog-share"><em style="color: red;font-size: 12px;">好文要分享:</em></div>';
        //正文
        html += '<div class="article-detail-content">';
        //前言
        html += '<div>&nbsp; &nbsp; <span style="color:#ee33ee;">前言</span>：'+val.summary+'</div>';
        html += '<hr />';
        //详细内容
        html += '<div style="text-indent: 2em;">'+convert(val.content)+'</div>';
        html += '</div>';
        html += '</div>';
    });

    return html;
}

/**
 * 语法转换
 */
function convert(text){
    //获取要转换的文字
    //创建实例
    var converter = new showdown.Converter({tables: true});
    //进行转换
    var html = converter.makeHtml(text);
    //展示到对应的地方  result便是id名称
    return html;
}
